package cn.mj.netty.serialize;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

public class ClientHandler extends ChannelInboundHandlerAdapter{
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		super.channelActive(ctx);
		System.out.println("-------------------");
//		ctx.writeAndFlush(new Request("0", "hello", "0"));
	}
	
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		if (msg instanceof Response) {
			Response response = (Response)msg;
			System.out.println(response);
//			ctx.writeAndFlush(new Request("1","哈哈，服务端，你很棒","1"));
		}else{
			System.out.println("client 无能力解析");
//			ctx.writeAndFlush(new Request("-1","客户端无法解析","-1")).addListener(ChannelFutureListener.CLOSE);
		}
		ReferenceCountUtil.release(msg);
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}
}
